package com.hotleproject.controller;

import com.hotleproject.pojo.Adminis;
import com.hotleproject.service.AdminiOperaService;
import com.hotleproject.service.AdminiService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;

@Controller
@RequestMapping("/users")
public class AdminiController {
    @Autowired
    private AdminiService adminiService;
    @RequestMapping("/findAdmini")
    private void findAdminiById(Adminis adminis,HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        String AdminiName = request.getParameter("AdminiName");
        String AdminiPassword = request.getParameter("AdminiPassword");
        adminis.setAdminiName(AdminiName);
        adminis.setAdminiPassword(AdminiPassword);
        response.setContentType("text/html;charset=gb2312");
        PrintWriter out = response.getWriter();
        adminis = this.adminiService.findAdminiById(AdminiName,AdminiPassword);
        if (adminis != null) {
            session.setAttribute("adminis",adminis.getAdminiName());
            out.print("<script language=\"javascript\">alert('登录成功，进入后台管理！');window.location.href='/users/sessionAdminiDeal'</script>");

//            mav.setViewName("manage");
//            return mav;
        } else {
            out.print("<script language=\"javascript\">alert('用户名或密码错误，请重新登录！');window.location.href='/users/Administration'</script>");
//            session.setAttribute("errormsg", "账号或密码错误!请重新输入");
        }
//        mav.setViewName("UserLogin");
//        return mav;

    }
    @Autowired
    private AdminiOperaService adminiOperaService;
    @RequestMapping("/sessionAdminiDeal")
    public String sessionAdminiDeal(Model model, HttpSession session){
        String adminis=session.getAttribute("adminis").toString();

//        String AdminiName = session.getAttribute("adminis").toString();

        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String operationDate = df.format(new Date());



        if(adminis.equals("Admini")){
            String operationThing = "最高管理员" + adminis +"在"+operationDate+"登录了。";
            this.adminiOperaService.insertope(adminis, operationThing, operationDate);
            model.addAttribute("adminis",adminis);
            return "manage";
        }

        else{
            String operationThing = "酒店管理员" + adminis +"在"+operationDate+"登录了。";
            this.adminiOperaService.insertope(adminis, operationThing, operationDate);
            model.addAttribute("adminis",adminis);
            return "SingleAdminimanage";
        }

    }




}
